/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   CSwitchData.cpp
 * Author: Administrator
 * 
 * Created on 2018年11月19日, 下午5:20
 */

#include "CSwitchData.h"
#include "PublicLog.h"
#include "SnmpClient.h"
#include "PublicSnmpCommon.h"
#include "PublicMySql.h"
#include "Common.h"
#include "SnmpApi.h"
#include "SnmpClient.h"
#include "SnmpResultApi.h"
//ARP表
static const SnmpOidInfo OID_ipNetToMediaTable={"ipNetToMediaTable",{1,3,6,1,2,1,4,22,1},9};
static const SnmpOidInfo OID_ipNetToMediaPhysAddress={"ipNetToMediaPhysAddress",{1,3,6,1,2,1,4,22,1,2},10,ASN_OCTET_STR};
static const SnmpOidInfo OID_ipNetToMediaType={"ipNetToMediaType",{1,3,6,1,2,1,4,22,1,4},10,ASN_INTEGER};
//ARPV6表
static const SnmpOidInfo OID_ipv6NetToMediaTable={"ipv6NetToMediaTable",{1,3,6,1,2,1,55,1,12,1},10};
static const SnmpOidInfo OID_ipv6NetToMediaPhysAddress={"ipv6NetToMediaPhysAddress",{1,3,6,1,2,1,55,1,12,1,2},11,ASN_OCTET_STR};
static const SnmpOidInfo OID_ipv6NetToMediaType={"ipv6NetToMediaType",{1,3,6,1,2,1,55,1,12,1,3},11,ASN_INTEGER};
//IP表
static const SnmpOidInfo OID_ipAdEntIfIndex={"ipAdEntIfIndex",{1,3,6,1,2,1,4,20,1,2},10,ASN_INTEGER};
static const SnmpOidInfo OID_ipAdEntNetMask={"ipAdEntNetMask",{1,3,6,1,2,1,4,20,1,3},10,ASN_IPADDRESS};
//IPV6表
static const SnmpOidInfo OID_ipv6AddrPfxLength={"ipv6AddrPfxLength",{1,3,6,1,2,1,55,1,8,1,2},11,ASN_INTEGER};
//static const SnmpOidInfo OID_ipAdEntNetMask={"ipAdEntNetMask",{1,3,6,1,2,1,4,20,1,3},10,ASN_INTEGER};
//路由表
static const SnmpOidInfo OID_ipCidrRouteTable={"ipCidrRouteTable",{1,3,6,1,2,1,4,24,4},9};
static const SnmpOidInfo OID_ipCidrRouteIfIndex={"ipCidrRouteIfIndex",{1,3,6,1,2,1,4,24,4,1,5},11,ASN_INTEGER};
static const SnmpOidInfo OID_ipCidrRouteType={"ipCidrRouteType",{1,3,6,1,2,1,4,24,4,1,6},11,ASN_INTEGER};
static const SnmpOidInfo OID_ipCidrRouteProto={"ipCidrRouteProto",{1,3,6,1,2,1,4,24,4,1,7},11,ASN_INTEGER};
//路由表V6
static const SnmpOidInfo OID_ipv6RouteTable={"ipv6RouteTable",{1,3,6,1,2,1,55,1,11,1},10};
static const SnmpOidInfo OID_ipv6RouteIfIndex={"ipv6RouteIfIndex",{1,3,6,1,2,1,55,1,11,1,4},11,ASN_INTEGER};
static const SnmpOidInfo OID_ipv6RouteNextHop={"ipv6RouteNextHop",{1,3,6,1,2,1,55,1,11,1,5},11,ASN_OCTET_STR};
static const SnmpOidInfo OID_ipv6RouteType={"ipv6RouteType",{1,3,6,1,2,1,55,1,11,1,6},11,ASN_INTEGER};
static const SnmpOidInfo OID_ipv6RouteProto={"ipv6RouteProto",{1,3,6,1,2,1,55,1,11,1,7},11,ASN_INTEGER};

CSwitchData::CSwitchData(std::string ip){
	m_ip=ip;
	m_ss=NULL;
	memset(&m_sysinfo,0,sizeof(SNMPSYSPACKET));
}
CSwitchData::~CSwitchData(){
}
void CSwitchData::UpdateSwitchSysInfo(SNMPSYSPACKET sysinfo){
	m_sysinfo=sysinfo;
}
void CSwitchData::OpenSession(){
	CloseSession();
	m_ss=OpenSnmp(m_ip.c_str());
}
void CSwitchData::CloseSession(){
	m_ss=CloseSnmp(m_ss);
}
void CSwitchData::UpDateSwitchData(int flag){
	int debugmode=SWITCH_DATA_FLAG_DEBUG&flag;
	if(debugmode)INFO("UpDateSwitchData FLAG:0x%x",flag);
	OpenSession();
	if(debugmode)INFO("ipNetToMediaTable Start");
	if(SWITCH_DATA_FLAG_ARPTABLE&flag){
		UpDateipNetToMediaTable();
		if(m_sysinfo.ipv6Forwarding==1){
			if(debugmode)INFO("ipv6NetToMediaTable Start");
			UpDateipv6NetToMediaTable();
		}
	}
	if(debugmode)INFO("ipAddrTable Start");
	if(SWITCH_DATA_FLAG_IPTABLE&flag){	
		UpDateipAddrTable();
		if(m_sysinfo.ipv6Forwarding==1){
			if(debugmode)INFO("ipv6AddrTable Start");
			UpDateipv6AddrTable();
		}
	}
	if(debugmode)INFO("ipRouteTable Start");
	if(SWITCH_DATA_FLAG_ROUTETABLE&flag){
		UpDateipRouteTable();
		if(m_sysinfo.ipv6RouteNumber>0){
			if(debugmode)INFO("ipv6RouteTable Start");
			UpDateipv6RouteTable();
		}
	}
	if(debugmode)INFO("FdbTable Start");
	if(SWITCH_DATA_FLAG_FDBTABLE&flag){
		UpDateFdbTable();
	}
	if(debugmode)INFO("BasePortTable Start");
	if(SWITCH_DATA_FLAG_BASEPORTTABLE&flag){
		UpDateBasePortTable();
	}
	if(debugmode)INFO("UpDateSwitchData End");
	CloseSession();
}
void CSwitchData::UpLoadToServer(){
	//INFO("UpLoadToServer");
	m_mysql=OpenMySQL(MYSQL_BASE);
	UpLoadipNetToMediaTable();
	UpLoadipv6NetToMediaTable();
	UpLoadipAddrTable();
	UpLoadipv6AddrTable();
	UpLoadipRouteTable();
	UpLoadipv6RouteTable();
	UpLoadFdbTable();
	UpLoadBasePortTable();
	CloseMySQL(m_mysql);
}
void CSwitchData::UpDateipNetToMediaTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkallex(m_ss,OID_ipNetToMediaPhysAddress,OID_ipNetToMediaType);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			MAPIPNETTOMEDIATABLE::iterator it=m_ipNetToMediaTable.end();
			do{
				int rootlen=OID_ipNetToMediaTable.oidLen;
				ipNetToMediaTable info;
				memset(&info,0,sizeof(ipNetToMediaTable));
				info.ipNetToMediaIfIndex=result->name[rootlen+1];
				OidReadIP(result->name,rootlen+2,info.ipNetToMediaNetAddress);
				it=m_ipNetToMediaTable.find(info.ipNetToMediaNetAddress);
				if(it==m_ipNetToMediaTable.end()){
					m_ipNetToMediaTable.insert(std::pair<std::string,ipNetToMediaTable>(info.ipNetToMediaNetAddress,info));
					it=m_ipNetToMediaTable.find(info.ipNetToMediaNetAddress);
				}
			}while(0);
			if(it!=m_ipNetToMediaTable.end()){
				if(OID_COMPARE(OID_ipNetToMediaPhysAddress,result)==0){
					ReadMacFromResult(result,OID_ipNetToMediaPhysAddress,it->second.ipNetToMediaPhysAddress);
				}else
				if(OID_COMPARE(OID_ipNetToMediaType,result)==0){
					ReadCount32FromResult(result,OID_ipNetToMediaType,it->second.ipNetToMediaType);
				}
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}
}
void CSwitchData::UpDateipv6NetToMediaTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkallex(m_ss,OID_ipv6NetToMediaPhysAddress,OID_ipv6NetToMediaType);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			MAPIPV6NETTOMEDIATABLE::iterator it=m_ipv6NetToMediaTable.end();
			do{
				int rootlen=OID_ipv6NetToMediaTable.oidLen;
				ipv6NetToMediaTable info;
				memset(&info,0,sizeof(ipv6NetToMediaTable));
				info.ipv6NetToMediaIfIndex=result->name[rootlen+1];
				OidReadIPV6(result->name,rootlen+2,info.ipv6NetToMediaNetAddress);
				it=m_ipv6NetToMediaTable.find(info.ipv6NetToMediaNetAddress);
				if(it==m_ipv6NetToMediaTable.end()){
					m_ipv6NetToMediaTable.insert(std::pair<std::string,ipv6NetToMediaTable>(info.ipv6NetToMediaNetAddress,info));
					it=m_ipv6NetToMediaTable.find(info.ipv6NetToMediaNetAddress);
				}
			}while(0);
			if(it!=m_ipv6NetToMediaTable.end()){
				if(OID_COMPARE(OID_ipv6NetToMediaPhysAddress,result)==0){
					ReadMacFromResult(result,OID_ipv6NetToMediaPhysAddress,it->second.ipv6NetToMediaPhysAddress);
				}else
				if(OID_COMPARE(OID_ipv6NetToMediaType,result)==0){
					ReadCount32FromResult(result,OID_ipv6NetToMediaType,it->second.ipv6NetToMediaType);
				}
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}
}
void CSwitchData::UpDateipAddrTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkallex(m_ss,OID_ipAdEntIfIndex,OID_ipAdEntNetMask);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			MAPIPADDRTABLE::iterator it=m_ipAddrTable.end();
			do{
				int rootlen=OID_ipAdEntIfIndex.oidLen;
				ipAddrTable info;
				memset(&info,0,sizeof(ipAddrTable));
				OidReadIP(result->name,rootlen+0,info.ipAdEntAddr);		
				it=m_ipAddrTable.find(info.ipAdEntAddr);
				if(it==m_ipAddrTable.end()){
					m_ipAddrTable.insert(std::pair<std::string,ipAddrTable>(info.ipAdEntAddr,info));
					it=m_ipAddrTable.find(info.ipAdEntAddr);
				}	
			}while(0);
			if(it!=m_ipAddrTable.end()){
				if(OID_COMPARE(OID_ipAdEntIfIndex,result)==0){
					ReadCount32FromResult(result,OID_ipAdEntIfIndex,it->second.ipAdEntIfIndex);
				}else
				if(OID_COMPARE(OID_ipAdEntNetMask,result)==0){	
					ReadIPFromResult(result,OID_ipAdEntNetMask,it->second.ipAdEntNetMask);
				}
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}
}
void CSwitchData::UpDateipv6AddrTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkall(m_ss,OID_ipv6AddrPfxLength);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			MAPIPV6ADDRTABLE::iterator it=m_ipv6AddrTable.end();
			do{
				int rootlen=OID_ipv6AddrPfxLength.oidLen;
				ipv6AddrTable info;
				memset(&info,0,sizeof(ipv6AddrTable));
				info.ipv6IfIndex=result->name[rootlen];
				OidReadIPV6(result->name,rootlen+1,info.ipv6AddrAddress);		
				it=m_ipv6AddrTable.find(info.ipv6AddrAddress);
				if(it==m_ipv6AddrTable.end()){
					m_ipv6AddrTable.insert(std::pair<std::string,ipv6AddrTable>(info.ipv6AddrAddress,info));
					it=m_ipv6AddrTable.find(info.ipv6AddrAddress);
				}	
			}while(0);
			if(it!=m_ipv6AddrTable.end()){
				if(OID_COMPARE(OID_ipv6AddrPfxLength,result)==0){
					ReadCount32FromResult(result,OID_ipv6AddrPfxLength,it->second.ipv6AddrPfxLength);
				}
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}
}
void AddipCidrRouteRequest(netsnmp_variable_list**varlist,SnmpOidInfo OidInfo,const char*Dest,const char*Mask,const char*NextHup){
    OidAddIP(OidInfo,Dest);
    OidAddIP(OidInfo,Mask);
    OidAddNum(OidInfo,0);
    OidAddIP(OidInfo,NextHup);
	AddSnmpRequestByNull(varlist,OidInfo);
//  snmp_varlist_add_variable(varlist,OidInfo.oidName,OidInfo.oidLen,ASN_NULL,NULL,0);
}
void CSwitchData::UpDateipRouteTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkall(m_ss,OID_ipCidrRouteIfIndex);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			ipRouteTable info;
			memset(&info,0,sizeof(ipRouteTable));
			int rootlen=OID_ipCidrRouteIfIndex.oidLen;
			OidReadIP(result->name,rootlen+0,info.ipRouteDest);
			OidReadIP(result->name,rootlen+4,info.ipRouteMask);
			OidReadIP(result->name,rootlen+9,info.ipRouteNextHop);	
			ReadCount32FromResult(result,OID_ipCidrRouteIfIndex,info.ipRouteifIndex);
			if(m_ipRouteTable.find(info.ipRouteNextHop)!=m_ipRouteTable.end()){
				WARN("IP:%s ipRouteTable 发现重复ipRouteNextHop:%s",m_ip.c_str(),info.ipRouteNextHop);
			}else{
				m_ipRouteTable.insert(std::pair<std::string,ipRouteTable>(info.ipRouteNextHop,info));
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
		result=NULL;
	}else{
		WRONG("IP:%s 采集 ipRouteTable 采集失败1",m_ip.c_str());
		return;
	}
///////////////////////////////////////////////////
	MAPIPROUTETABLE::iterator it=m_ipRouteTable.begin();
	while(it!=m_ipRouteTable.end()){
		AddipCidrRouteRequest(&result,OID_ipCidrRouteType,it->second.ipRouteDest,it->second.ipRouteMask,it->second.ipRouteNextHop);
		AddipCidrRouteRequest(&result,OID_ipCidrRouteProto,it->second.ipRouteDest,it->second.ipRouteMask,it->second.ipRouteNextHop);
		it++;
	}
	if(netsnmp_sess_query_get_ex(result,m_ss)==0){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			int rootlen=OID_ipCidrRouteIfIndex.oidLen;
			char ipRouteNextHop[16]={0};
			OidReadIP(result->name,rootlen+9,ipRouteNextHop);	
			MAPIPROUTETABLE::iterator it=m_ipRouteTable.find(ipRouteNextHop);
			if(it!=m_ipRouteTable.end()){
				if(OID_COMPARE(OID_ipCidrRouteType,result)==0){
					ReadCount32FromResult(result,OID_ipCidrRouteType,it->second.ipRouteType);
				}else
				if(OID_COMPARE(OID_ipCidrRouteProto,result)==0){
					ReadCount32FromResult(result,OID_ipCidrRouteProto,it->second.ipRouteType);
				}
			}else{
				WRONG("IP:%s 采集 ipRouteTable 采集失败",m_ip.c_str());
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}else{
		WRONG("IP:%s 采集 ipRouteTable 采集失败2",m_ip.c_str());
		SAFE_FREE_VARBIND(result);
    }
}
void CSwitchData::UpDateipv6RouteTable(){
	netsnmp_variable_list*result=netsnmp_sess_query_walkallex(m_ss,OID_ipv6RouteIfIndex,OID_ipv6RouteProto);
	if(result!=NULL){
		netsnmp_variable_list*resultsave=result;
		while(result!=NULL){
			MAPIPV6ROUTETABLE::iterator it=m_ipv6RouteTable.end();
			do{
				int rootlen=OID_ipv6RouteTable.oidLen;
				ipv6RouteTable info;
				memset(&info,0,sizeof(ipv6RouteTable));
				OidReadIPV6(result->name,rootlen+1,info.ipv6RouteDest);
				info.ipv6RoutePfxLength=result->name[rootlen+17];
				info.ipv6RouteIndex=result->name[rootlen+18];
				char key[128]={0};
				snprintf(key,sizeof(key),"%s%x%x",info.ipv6RouteDest,info.ipv6RoutePfxLength,info.ipv6RouteIndex);		
				it=m_ipv6RouteTable.find(key);
				if(it==m_ipv6RouteTable.end()){
					m_ipv6RouteTable.insert(std::pair<std::string,ipv6RouteTable>(key,info));
					it=m_ipv6RouteTable.find(key);
				}	
			}while(0);
			if(it!=m_ipv6RouteTable.end()){
				if(OID_COMPARE(OID_ipv6RouteIfIndex,result)==0){
					ReadCount32FromResult(result,OID_ipv6RouteIfIndex,it->second.ipv6RouteifIndex);
				}else
				if(OID_COMPARE(OID_ipv6RouteNextHop,result)==0){	
					ReadIPV6FromResult(result,OID_ipv6RouteNextHop,it->second.ipv6RouteNextHop);
					//printf("dest:%s pfl:%u index:%u len:%d type:%hhu nxt:%s\n",it->second.ipv6RouteDest,
					//	it->second.ipv6RoutePfxLength,it->second.ipv6RouteIndex,
					//	result->val_len,result->type,it->second.ipv6RouteNextHop);
				}else
				if(OID_COMPARE(OID_ipv6RouteType,result)==0){
					ReadCount32FromResult(result,OID_ipv6RouteType,it->second.ipv6RouteType);
				}else
				if(OID_COMPARE(OID_ipv6RouteProto,result)==0){	
					ReadCount32FromResult(result,OID_ipv6RouteProto,it->second.ipv6RouteProto);
				}
			}
			result=result->next_variable;
		}
		SAFE_FREE_VARBIND(resultsave);
	}
}
void CSwitchData::UpLoadipNetToMediaTable(){
	const char*Head="INSERT INTO`ipNetToMediaTable`(`IP`,`ipNetToMediaNetAddress`,`ipNetToMediaPhysAddress`,`ipNetToMediaIfIndex`,`ipNetToMediaType`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipNetToMediaPhysAddress`=VALUES(`ipNetToMediaPhysAddress`),"
					"`ipNetToMediaIfIndex`=VALUES(`ipNetToMediaIfIndex`),"
					"`ipNetToMediaType`=VALUES(`ipNetToMediaType`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPNETTOMEDIATABLE::iterator it=m_ipNetToMediaTable.begin();
	while(it!=m_ipNetToMediaTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s','%s',%u,%u)",m_ip.c_str(),
			it->second.ipNetToMediaNetAddress,it->second.ipNetToMediaPhysAddress,
			it->second.ipNetToMediaIfIndex,it->second.ipNetToMediaType);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}

void CSwitchData::UpLoadipv6NetToMediaTable(){
	const char*Head="INSERT INTO`ipv6NetToMediaTable`(`IP`,`ipv6NetToMediaNetAddress`,`ipv6NetToMediaPhysAddress`,`ipv6NetToMediaIfIndex`,`ipv6NetToMediaType`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipv6NetToMediaPhysAddress`=VALUES(`ipv6NetToMediaPhysAddress`),"
					"`ipv6NetToMediaIfIndex`=VALUES(`ipv6NetToMediaIfIndex`),"
					"`ipv6NetToMediaType`=VALUES(`ipv6NetToMediaType`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPV6NETTOMEDIATABLE::iterator it=m_ipv6NetToMediaTable.begin();
	while(it!=m_ipv6NetToMediaTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s','%s',%u,%u)",m_ip.c_str(),
			it->second.ipv6NetToMediaNetAddress,it->second.ipv6NetToMediaPhysAddress,
			it->second.ipv6NetToMediaIfIndex,it->second.ipv6NetToMediaType);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}


void CSwitchData::UpLoadipAddrTable(){
	const char*Head="INSERT INTO`ipAddrTable`(`IP`,`ipAdEntAddr`,`ipAdEntNetMask`,`ipAdEntIfIndex`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipAdEntNetMask`=VALUES(`ipAdEntNetMask`),"
					"`ipAdEntIfIndex`=VALUES(`ipAdEntIfIndex`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPADDRTABLE::iterator it=m_ipAddrTable.begin();
	while(it!=m_ipAddrTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s','%s',%u)",m_ip.c_str(),it->second.ipAdEntAddr,it->second.ipAdEntNetMask,it->second.ipAdEntIfIndex);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}

void CSwitchData::UpLoadipv6AddrTable(){
	const char*Head="INSERT INTO`ipv6AddrTable`(`IP`,`ipv6AddrAddress`,`ipv6AddrPfxLength`,`ipv6IfIndex`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipv6AddrPfxLength`=VALUES(`ipv6AddrPfxLength`),"
					"`ipv6IfIndex`=VALUES(`ipv6IfIndex`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPV6ADDRTABLE::iterator it=m_ipv6AddrTable.begin();
	while(it!=m_ipv6AddrTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s',%u,%u)",m_ip.c_str(),it->second.ipv6AddrAddress,
			it->second.ipv6AddrPfxLength,it->second.ipv6IfIndex);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}

void CSwitchData::UpLoadipRouteTable(){
	const char*Head="INSERT INTO`ipRouteTable`(`IP`,`ipRouteDest`,`ipRouteMask`,`ipRouteifIndex`,`ipRouteNextHop`,`ipRouteType`,`ipRouteProto`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipRouteDest`=VALUES(`ipRouteDest`),"
					"`ipRouteMask`=VALUES(`ipRouteMask`),"
					"`ipRouteifIndex`=VALUES(`ipRouteifIndex`),"
					"`ipRouteNextHop`=VALUES(`ipRouteNextHop`),"
					"`ipRouteType`=VALUES(`ipRouteType`),"
					"`ipRouteProto`=VALUES(`ipRouteProto`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPROUTETABLE::iterator it=m_ipRouteTable.begin();
	while(it!=m_ipRouteTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s','%s',%u,'%s',%u,%u)",m_ip.c_str(),
			it->second.ipRouteDest,it->second.ipRouteMask,it->second.ipRouteifIndex,
			it->second.ipRouteNextHop,it->second.ipRouteType,it->second.ipRouteProto);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}

void CSwitchData::UpLoadipv6RouteTable(){
	const char*Head="INSERT INTO`ipv6RouteTable`(`IP`,`ipv6RouteDest`,`ipv6RoutePfxLength`,`ipv6RouteIndex`,`ipv6RouteifIndex`,`ipv6RouteNextHop`,`ipv6RouteType`,`ipv6RouteProto`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`ipv6RouteDest`=VALUES(`ipv6RouteDest`),"
					"`ipv6RoutePfxLength`=VALUES(`ipv6RoutePfxLength`),"
					"`ipv6RouteIndex`=VALUES(`ipv6RouteIndex`),"
					"`ipv6RouteifIndex`=VALUES(`ipv6RouteifIndex`),"
					"`ipv6RouteNextHop`=VALUES(`ipv6RouteNextHop`),"
					"`ipv6RouteType`=VALUES(`ipv6RouteType`),"	
					"`ipv6RouteProto`=VALUES(`ipv6RouteProto`);";
	int Count=0;
	std::string sqlbuff;
	MAPIPV6ROUTETABLE::iterator it=m_ipv6RouteTable.begin();
	while(it!=m_ipv6RouteTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s',%u,%u,%u,'%s',%u,%u)",m_ip.c_str(),
			it->second.ipv6RouteDest,it->second.ipv6RoutePfxLength,it->second.ipv6RouteIndex,it->second.ipv6RouteifIndex,
			it->second.ipv6RouteNextHop,it->second.ipv6RouteType,it->second.ipv6RouteProto);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}

void CSwitchData::UpLoadFdbTable(){
	const char*Head="INSERT INTO`FdbTable`(`IP`,`FdbAddress`,`FdbPort`,`FdbStatus`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`FdbPort`=VALUES(`FdbPort`),"
					"`FdbStatus`=VALUES(`FdbStatus`);";
	int Count=0;
	std::string sqlbuff;
	MAPFDBTABLE::iterator it=m_FdbTable.begin();
	while(it!=m_FdbTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s','%s',%u,%u)",m_ip.c_str(),it->second.FdbAddress,it->second.FdbPort,it->second.FdbStatus);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}
void CSwitchData::UpLoadBasePortTable(){
	const char*Head="INSERT INTO`BasePortTable`(`IP`,`BasePort`,`BasePortIfIndex`)VALUES";
	const char*End="ON DUPLICATE KEY UPDATE"
					"`BasePortIfIndex`=VALUES(`BasePortIfIndex`);";
	int Count=0;
	std::string sqlbuff;
	MAPUINTLIST::iterator it=m_BasePortTable.begin();
	while(it!=m_BasePortTable.end()){
		char tmp[2048]={0};
		snprintf(tmp,2048,"('%s',%u,%u)",m_ip.c_str(),it->first,it->second);
		if(Count==0){
			sqlbuff=Head;
		}else{
			sqlbuff+=",";
		}
		sqlbuff+=tmp;
		Count++;
		it++;
	}
	if(Count>0){
		sqlbuff+=End;		
		RunMySQL(m_mysql,sqlbuff.c_str());
	}	
}
